﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using BL;
using Entities;


namespace WindowsFormsApplication1
{
    public partial class frmKiemTraKTBTP : Form
    {
        DonHangBL donHangBL;
        BindingSource bsdh = new BindingSource();
        DataTable dttheodoidonhangchung = null;
        DataSet dstheodoidonhangchung;
        ThongTinDonHangBL thongTinDonHangBL;

        TheoDoiCongNhanBL theoDoiCongNhanBL;
        CongNhanBL congNhanBL;
        DataTable dtchoncongnhan = null;

        SanPhamBL sanPhamBL;
        BindingSource bssp = new BindingSource();
        DataTable dtSanPham = null;
        DataSet dsSanPham;

        DataTable dtMaDonHang;

        ComboBox currentSelectedCmbMaDonHang = null;
        ComboBox currentSelectedCmbMaSanPham = null;

        DA.BaseConnection.DataPacket dpDsTheoiDoiCongNhan;

        public frmKiemTraKTBTP()
        {
            InitializeComponent();
            theoDoiCongNhanBL = new TheoDoiCongNhanBL();
            congNhanBL = new CongNhanBL();
            thongTinDonHangBL = new ThongTinDonHangBL();
            donHangBL = new DonHangBL();
            sanPhamBL = new SanPhamBL();
        }

        private void KiemTraKTBTP_Load(object sender, EventArgs e)
        {
            //dttheodoidonhangchung = thongTinDonHangBL.chonDonHang().Tables[0];
            try
            {
                dtMaDonHang = thongTinDonHangBL.chonDonHang().Tables[0];

                cmbMaDonHang.DataSource = dtMaDonHang;
                cmbMaDonHang.DisplayMember = "MaDonHang";
                cmbMaDonHang.ValueMember = "MaDonHang";

                bsdh.DataSource = dtMaDonHang.DataSet.Tables[0];
                //  dgvKiemTraLoiSanPham.DataSource = bsdh;

                dtchoncongnhan = congNhanBL.chonCongNhan().Tables[0];
                cmbMaCongNhan.DataSource = dtchoncongnhan;
                cmbMaCongNhan.DisplayMember = "TenCongNhan";
                cmbMaCongNhan.ValueMember = "MaCongNhan";

                dpDsTheoiDoiCongNhan = theoDoiCongNhanBL.layDanhSachTheoDoiCongNhanDeThem();
                bssp.DataSource = dpDsTheoiDoiCongNhan.ds.Tables[0];
                dgvKiemTraLoiSanPham.DataSource = bssp;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }            
            //theoDoiCongNhanBL.capNhatLoiCongNhan("1", "L-18", "1", "6", 2, "2013/12/12", "2012/12/12");
            //theoDoiCongNhanBL.capNhatLoiCongNhan("1", "L-18", "1", "1", 20, "2013/12/12", "2012/12/12");
        }

        private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
            if (QuanLyTaiKhoan.taiKhoanONhomXemThemXoaSuaPhongChatLuong() == true)
            {
                if (dgvKiemTraLoiSanPham.CurrentRow.Cells["cmbMaDonHang"].Value != null
                    && dgvKiemTraLoiSanPham.CurrentRow.Cells["cmbMaSanPham"].Value != null
                    && dgvKiemTraLoiSanPham.CurrentRow.Cells["cmbMaCongNhan"].Value != null
                    && dtpNgaySanXuat.Value != null
                    && dtpNgayKiemTra.Value != null)
                {

                    //lay kieu string
                    //dgvKiemTraLoiSanPham.CurrentRow.Cells[0].Value.ToString();                    
                    string maDonHang = dgvKiemTraLoiSanPham.CurrentRow.Cells["cmbMaDonHang"].Value.ToString();
                    string maSanPham = dgvKiemTraLoiSanPham.CurrentRow.Cells["cmbMaSanPham"].Value.ToString();
                    string maCongNhan = dgvKiemTraLoiSanPham.CurrentRow.Cells["cmbMaCongNhan"].Value.ToString();
                    string ngaySanXuat = dtpNgaySanXuat.Value.ToString("yyyy/MM/dd");
                    string ngayKiemTra = dtpNgayKiemTra.Value.ToString("yyyy/MM/dd");

                    //// lay du lieu theo so
                    //Int32.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());
                    //int maLoi = 0;
                    for (int j = 6; j < 22; j++)
                    {
                        try
                        {
                            Int32 soLoi = Int32.Parse(dgvKiemTraLoiSanPham.CurrentRow.Cells[j].Value.ToString());
                            //Int32 soLoi = Int32.Parse(dgvKiemTraLoiSanPham.CurrentRow.Cells[dgvKiemTraLoiSanPham.Columns["Chop"].Index + maLoi].Value.ToString());
                            if (!dgvKiemTraLoiSanPham.CurrentRow.Cells[j].Value.ToString().Equals(""))
                            {
                                theoDoiCongNhanBL.capNhatLoiCongNhan(maDonHang, maSanPham, maCongNhan, (j - 5) + "", soLoi, ngaySanXuat, ngayKiemTra);
                            }
                           // maLoi++;
                        }
                        catch (Exception ex)
                        {

                        }
                    }
                }
                //if (!dataGridView1.CurrentRow.Cells[10].Value.ToString().Equals(""))
                //{
                //    //theoDoiCongNhanBL.capNhatLoiCongNhan("maDonHang", "maSanPham", "maCongNhan", "2", 20, "2013/12/12", "2012/12/12");
                //}
                }
            }
                catch (Exception ex)
                {
                   MessageBox.Show(ex.Message);
                }
        }
        private void dgvKiemTraLoiSanPham_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            //if (dgvKiemTraLoiSanPham.CurrentCell.OwningColumn.Name.Equals("cmbMaSanPham"))
            //{
            //    currentSelectedCmbMaSanPham = e.Control as ComboBox;
            //    ComboBox tmpCmbMaSanPham = e.Control as ComboBox;
            //    tmpCmbMaSanPham.Name = "cmbMaSanPham";
            //    tmpCmbMaSanPham.SelectedIndexChanged += cmbMaSanPham_SelectedIndexChanged;
            //}

            //if (dgvKiemTraLoiSanPham.CurrentCell.OwningColumn.Name.Equals("cmbMaDonHang"))
            //{
                
            //    currentSelectedCmbMaDonHang = e.Control as ComboBox;
            //    ComboBox tmpCmbMaDonHang = e.Control as ComboBox;
            //    tmpCmbMaDonHang.Name = "cmbMaDonHang";
            //    tmpCmbMaDonHang.SelectedIndexChanged -= cmbMaDonHang_SelectedIndexChanged;
            //    tmpCmbMaDonHang.SelectedIndexChanged += cmbMaDonHang_SelectedIndexChanged;
            //}

        }

        private void cmbMaDonHang_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }

        private void cmbMaSanPham_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (currentSelectedCmbMaDonHang != null)
                {
                    DataTable dtSanPham = sanPhamBL.thongTinSanPham(currentSelectedCmbMaSanPham.SelectedValue.ToString()).Tables[0];
                    dgvKiemTraLoiSanPham.CurrentRow.Cells["txtSoMau"].Value = dtSanPham.Rows[0][1].ToString();
                    dgvKiemTraLoiSanPham.CurrentRow.Cells["txtKichThuoc"].Value = dtSanPham.Rows[0][0].ToString();
                    currentSelectedCmbMaDonHang = null;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }            
        }

        private void dgvKiemTraLoiSanPham_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == dgvKiemTraLoiSanPham.Columns["cmbMaDonHang"].Index)
                {
                    cmbMaSanPham.Items.Clear();
                    DataTable dtSanPhamTheoDonHang = donHangBL.laySanPhamTheoMaDonHang(dgvKiemTraLoiSanPham[e.ColumnIndex, e.RowIndex].Value.ToString()).Tables[0];
                    foreach (DataRow item in dtSanPhamTheoDonHang.Rows)
                    {
                        ((DataGridViewComboBoxCell)dgvKiemTraLoiSanPham[dgvKiemTraLoiSanPham.Columns["cmbMaSanPham"].Index, e.RowIndex]).Items.Add(item["MaSanPham"]);
                    }
                }

                if (e.ColumnIndex == dgvKiemTraLoiSanPham.Columns["cmbMaSanPham"].Index)
                {
                    DataTable dtSanPham = sanPhamBL.thongTinSanPham(dgvKiemTraLoiSanPham[e.ColumnIndex, e.RowIndex].Value.ToString()).Tables[0];
                    dgvKiemTraLoiSanPham.CurrentRow.Cells["txtSoMau"].Value = dtSanPham.Rows[0][1].ToString();
                    dgvKiemTraLoiSanPham.CurrentRow.Cells["txtKichThuoc"].Value = dtSanPham.Rows[0][0].ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }            
        }
    }
}
